package com.example.dnfgamercommunitybackend.controller;

import com.example.dnfgamercommunitybackend.entity.Article;
import com.example.dnfgamercommunitybackend.service.ArticleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @PreAuthorize("hasAuthority('user:add_article')")
    @PostMapping("/article/addArticle")
    //新增文章
    public String addArticle(@RequestBody Article article, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return articleService.addArticle(article,token);
    }

    @PreAuthorize("hasAuthority('user:delete_article')")
    @GetMapping("/article/deleteArticle")
    public String deleteArticle(@RequestParam("id") int id){
        return articleService.deleteArticleById(id);
    }

    @PreAuthorize("hasAuthority('user:update_article')")
    @PostMapping("/article/updateArticle")
    public String updateArticle(@RequestBody Article article,HttpServletRequest request){
        return articleService.updateArticle(article,request.getHeader("Authorization"));
    }

    @PreAuthorize("hasAuthority('user:get_article_detail')")
    @GetMapping("/article/getArticleDetail")
    public String getArticleDetail(@RequestParam("articleId") int articleId,HttpServletRequest httpServletRequest){
        return articleService.getArticleDetail(articleId,httpServletRequest.getHeader("Authorization"));
    }

    @PreAuthorize("hasAuthority('user:artilce_get_all_artilce')")
    @GetMapping("/article/getAllArticle")
    public String getAllArticle(){
        return articleService.getAllArticle();
    }

    @PreAuthorize("hasAuthority('user:article_get_user_article')")
    @GetMapping("/article/getUserArticle")
    public String getUserArticleByUserId(HttpServletRequest request){
        return articleService.getUserArticleByUserId(request.getHeader("Authorization"));
    }

    @PreAuthorize("hasAuthority('user:article_search_article')")
    @GetMapping("/article/getArticleByKeyword")
    public String getArticleByKeyWord(@RequestParam("keyword") String keyword){
        return articleService.getArticleByKeyword(keyword);
    }
}
